package com.interview.salesorder.service;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.interview.core.page.Page;
import com.interview.orderdetail.entity.OrderDetail;
import com.interview.orderdetail.entity.OrderDetailListForm;
import com.interview.orderdetail.query.OrderlistConditionQuery;
import com.interview.orderdetail.repository.OrderlistRepository;
import com.interview.salesorder.entity.SalesOrder;
import com.interview.salesorder.query.SalesOrderConditionQuery;
import com.interview.salesorder.repository.SalesorderRepository;

@Service
public class SalesorderService {
	@Resource
	SalesorderRepository salesorderRepository;

	

	@Resource
	OrderlistRepository orderlistRepository;

	
	public void deleteOrderList(String id) {
		orderlistRepository.deleteOrderList(id);
	}

	public List<OrderDetail> findOrderListByOrderlistConditionQuery(
			OrderlistConditionQuery conditionQuery) {
		
		return orderlistRepository
				.findOrderListByOrderlistConditionQuery(conditionQuery);
		
	}

	public OrderDetail findOneOrderList(String id) {
		
		return orderlistRepository.findOneOrderList(id);
	}

	public OrderDetail saveOrderList(OrderDetail orderlist) {
		return orderlistRepository.saveOrderList(orderlist);
	}

	public OrderDetail updateOrderList(OrderDetail orderList) {
		return orderlistRepository.updateOrderList(orderList);
	}

	public List<OrderDetail> findAllOrderList() {
		return orderlistRepository.findAllOrderList();
	}

	// ------------------------------------------------------------------------------------
	
	public List<SalesOrder> findAllSalesOrder() {
		return salesorderRepository.findAllSalesOrder();
	}

	public SalesOrder findOneSalesOrder(String id) {
		return salesorderRepository.findOneSalesOrder(id);
	}

	public SalesOrder updateSalesOrder(SalesOrder order) {
		return salesorderRepository.updateSalesOrder(order);
	}

	public SalesOrder saveSalesOrder(SalesOrder order) {
		return salesorderRepository.saveSalesOrder(order);
	}

	public void deleteSalesOrder(String id) {
		salesorderRepository.deleteSalesOrder(id);
	}

	public Page findSalesOrderBySalesOrderConditionQuery(
			SalesOrderConditionQuery conditionQuery) {
		return salesorderRepository
				.findSalesOrderBySalesOrderConditionQuery(conditionQuery);
	}
	
	// ------------------------------------------------------------------------------------------
	
	public SalesOrder updateBySalesOrderAndOrderListForm(SalesOrder salesOrder,
			OrderDetailListForm listForm) {

		if (listForm != null & listForm.getOrderDetails().size() != 0) {

			List<OrderDetail> listForms = listForm.getOrderDetails();

			for (Iterator iterator = listForms.iterator(); iterator.hasNext();) {

				OrderDetail orderList = (OrderDetail) iterator.next();

				Double discount = orderList.getDiscount();
				Double productcost = orderList.getProductCost();
				String productId = orderList.getProductId();
				String productName = orderList.getProductName();
				Integer productNum = orderList.getProductNum();
				Double productprice = orderList.getProductPrice();

				if (discount == null & productcost == null & productId == null
						& productName == null & productNum == null
						& productprice == null) {

					iterator.remove();

				}
			}
			
			salesOrder.setOrderList(listForms);
			return updateSalesOrder(salesOrder);
			
		} else {
			
			return updateSalesOrder(salesOrder);
			
		}

	}
	public SalesOrder saveBySalesOrderAndOrderListForm(SalesOrder salesOrder,
			OrderDetailListForm listForm) {

		if (listForm != null || listForm.getOrderDetails().size() != 0) {

			List<OrderDetail> listForms = listForm.getOrderDetails();

			for (Iterator iterator = listForms.iterator(); iterator.hasNext();) {

				OrderDetail orderList = (OrderDetail) iterator.next();

				Double discount = orderList.getDiscount();
				Double productcost = orderList.getProductCost();
				String productId = orderList.getProductId();
				String productName = orderList.getProductName();
				Integer productNum = orderList.getProductNum();
				Double productprice = orderList.getProductPrice();

				if (discount == null & productcost == null & productId == null
						& productName == null & productNum == null
						& productprice == null) {

					iterator.remove();

				}
			}
			
			salesOrder.setOrderList(listForms);
			return saveSalesOrder(salesOrder);
			
		} else {
			
			return saveSalesOrder(salesOrder);
			
		}

	}
	//-------------------------------------------------------------------------------------------
	
}
